{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "cdf9e03b-a513-4b81-a75e-243f38a3c84a",
   "metadata": {},
   "source": [
    "### clone代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3851d799-7162-4e73-acab-3c13cb1e43bd",
   "metadata": {},
   "outputs": [],
   "source": [
    "!git clone https://github.com/modelscope/modelscope-agent.git"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f1bcfef8-ab39-4b10-b19d-2dcdf12e9334",
   "metadata": {},
   "source": [
    "### 安装特定依赖"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "918bc71f-e908-46c5-94dd-d7d6246d3363",
   "metadata": {},
   "outputs": [],
   "source": [
    "! cd modelscope-agent && pip install -r requirements.txt"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5d206c2f",
   "metadata": {},
   "source": [
    "### 本地配置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c54a7a8e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "os.chdir('modelscope-agent/examples/tools')\n",
    "\n",
    "import sys\n",
    "sys.path.append('../../')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "16c11b1a",
   "metadata": {},
   "source": [
    "### API_KEY管理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "96d108cf",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "print('请输入DASHSCOPE_API_KEY')\n",
    "os.environ['DASHSCOPE_API_KEY'] = input()\n",
    "print('请输入ModelScope Token')\n",
    "os.environ['MODELSCOPE_API_TOKEN'] = input()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d90a88fc",
   "metadata": {},
   "source": [
    "### 构建agent并且使用langchain"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "52c2c8c6",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.tools import ShellTool\n",
    "from modelscope_agent.tools.langchain_proxy_tool import LangchainTool\n",
    "from modelscope_agent.agents import RolePlay\n",
    "from modelscope_agent.tools import TOOL_REGISTRY\n",
    "\n",
    "# All LangchainTool should be registered in such way, terminal is the tool name that will change by task\n",
    "TOOL_REGISTRY['terminal'] = LangchainTool\n",
    "\n",
    "role_template = '你是一个助手，试图用工具帮助人类解决问题。'\n",
    "\n",
    "llm_config = {\n",
    "    'model': 'qwen-max', \n",
    "    'model_server': 'dashscope',\n",
    "    }\n",
    "\n",
    "# ShellTool() use to initialize the terminal tool\n",
    "function_list = [\"code_interpreter\", {'terminal':ShellTool()}]\n",
    "bot = RolePlay(function_list=function_list,llm=llm_config,instruction=role_template)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c5a5cfcd",
   "metadata": {},
   "source": [
    "### 执行agent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bab9148d",
   "metadata": {},
   "outputs": [],
   "source": [
    "response = bot.run(\"请输出查看环境中git的版本号\", remote=False, print_info=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0150bcc7",
   "metadata": {},
   "outputs": [],
   "source": [
    "text = ''\n",
    "for chunk in response:\n",
    "    text += chunk\n",
    "print(text)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
